package com.cuit.zuke.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.zuke.common.util.Result;
import com.cuit.zuke.product.model.entity.Goods;
import com.cuit.zuke.product.model.entity.Stock;
import com.cuit.zuke.product.model.entity.Warehouse;
import com.cuit.zuke.product.model.vo.GoodsQueryVO;
import com.cuit.zuke.product.model.vo.StockQueryVO;
import com.cuit.zuke.product.model.vo.StockVO;
import com.cuit.zuke.product.service.StockService;
import com.cuit.zuke.product.service.WarehouseService;
import com.cuit.zuke.product.util.FileUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/product/stock")
public class StockController {

    @Autowired
    private StockService stockService;

    @Autowired
    private WarehouseService warehouseService;


    /**
     * 带条件的分页查询
     * @param page
     * @param limit
     * @param stockQueryVO
     * @return
     */
    @GetMapping(value = "list/{page}/{limit}")
    public Result listStock(@PathVariable Integer page,
                            @PathVariable Integer limit,
                            StockQueryVO stockQueryVO){
        Page<StockVO> stockVOPage = stockService.findPage(page, limit, stockQueryVO);
        List<Warehouse> warehouseList = warehouseService.findAll();
        Warehouse warehouse = new Warehouse();
        warehouse.setId(null);
        warehouse.setName("全部");
        warehouseList.add(0,warehouse);
        return Result.ok().put("data",stockVOPage).put("warehouseList",warehouseList);
    }

    /**
     * 根据id删除库存
     * @param id
     * @return
     */
    @PostMapping(value = "deleteById")
    public Result deleteStock(@RequestBody Long id){
        boolean flag = stockService.deleteById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 库存进货
     */
    @PostMapping(value = "addStock")
    public Result addStock(@RequestBody Stock stock) {
        boolean flag = stockService.addStock(stock);
        if (flag){
            return Result.ok();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改
     */
    @PostMapping(value = "updateStock")
    public Result update(@RequestBody Stock stock){
        boolean flag = stockService.updateStock(stock);
        if (flag){
            return Result.ok();
        }else {
            return Result.error("更新失败");
        }
    }

    /**
     * 根据id查找
     */
    @GetMapping(value = "getStockById/{id}")
    public Result getStockById(@PathVariable Long id){
        Stock stock = stockService.getById(id);
        return Result.ok().put("stock",stock);
    }

    /**
     * 导出excel
     */
    @GetMapping(value = "exportData")
    public void exportData(HttpServletResponse response){
        stockService.exportStockData(response);
    }

    /**
     * 根据warehouse_id得到stock
     */
    @GetMapping(value = "getByWarehouseId/{warehouseId}")
    public List<Stock> getByWarehouseIdCli(@PathVariable Integer warehouseId){
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warehouse_id",warehouseId);
        List<Stock> list = stockService.list(queryWrapper);
        return list;
    }

    /**
     * 减库存
     */
    @GetMapping(value = "reduceStock/{stockId}/{num}")
    void reduceStock(@PathVariable(value = "stockId") Long stockId,@PathVariable(value = "num") Integer num){
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",stockId);
        Stock stock = stockService.getOne(queryWrapper);
        Integer n = stock.getNum()-num;
        stock.setNum(n);
        stockService.update(stock,queryWrapper);
    }

}
